import 'dart:async';

import 'package:flutter/material.dart';
import 'package:shopping_group/base/base_constant.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:shopping_group/entity/widget_entity.dart';
import 'package:shopping_group/modle/person/bloc/bank_list_bloc.dart';
import 'package:shopping_group/res/colors.dart';
import 'package:shopping_group/res/dimens.dart';
import 'package:shopping_group/utils/object_util.dart';
import 'package:shopping_group/utils/widget_utils.dart';
import 'package:shopping_group/view/button_progress_view.dart';

class AddBankPage extends StatefulWidget {
  AddBankPage({
    Key key,
  }) : super(key: key);

  @override
  _AddBankPageState createState() => _AddBankPageState();
}

class _AddBankPageState extends State<AddBankPage> {
  // WidgetEntity _bankWidgetEntity = WidgetEntity();
  // List<CommunitySeletionResultEntity> _bankList = [];

  WidgetEntity _zhWidgetEntity = WidgetEntity();
  WidgetEntity _khhWidgetEntity = WidgetEntity();
  // WidgetEntity _zfbWidgetEntity = WidgetEntity();
  // List<CommunitySeletionResultEntity> _zfbList = [];

  // WidgetEntity _mrWidgetEntity = WidgetEntity();
  // List<CommunitySeletionResultEntity> _mrList = [];

  BankListBloc _bloc;
  StreamSubscription _refresSubscription;

  @override
  void initState() {
    super.initState();
    _bloc = BankListBloc();
    _bloc.bankTypeDate.add(null);
    // _bankList
    //   ..add(CommunitySeletionResultEntity()
    //     ..name = '请选择'
    //     ..id = '');
    //_bankWidgetEntity = WidgetUtils.initSpinner('账户类型', '', _bankList, '');
    _zhWidgetEntity = WidgetUtils.initEditText(
        '银行卡号', '', '请输入银行卡号', 'number', TextEditingController());
    _khhWidgetEntity = WidgetUtils.initEditText(
        '开户分行', '', '请输入银行卡开户分行', 'all', TextEditingController());
    // _mrList
    //   ..add(CommunitySeletionResultEntity()
    //     ..name = '请选择'
    //     ..id = '')
    //   ..add(CommunitySeletionResultEntity()
    //     ..name = '是'
    //     ..id = 'Y')
    //   ..add(CommunitySeletionResultEntity()
    //     ..name = '否'
    //     ..id = 'N');
    // _mrWidgetEntity = WidgetUtils.initSpinner('默认', '', _mrList, '');

    // _zfbList
    //   ..add(CommunitySeletionResultEntity()
    //     ..name = '否'
    //     ..id = 'N')
    //   ..add(CommunitySeletionResultEntity()
    //     ..name = '是'
    //     ..id = 'Y');
    // _zfbWidgetEntity = WidgetUtils.initSpinner('支付宝', '', _zfbList, '');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: WidgetUtils.showAppar(context, '新增银行卡号'),
      body: Container(
        decoration: new BoxDecoration(
          color: Colors.white,
        ),
        child: SingleChildScrollView(
          scrollDirection: Axis.vertical,
          reverse: false,
          padding: EdgeInsets.all(0.0),
          physics: BouncingScrollPhysics(),
          child: Column(
            children: <Widget>[
              // _bankTypelistView(),
              WidgetUtils.itmeStyleView('户主姓名', BaseConstant.name,
                  bewteenleft: 16),
              WidgetUtils.showEditText(_zhWidgetEntity),
              WidgetUtils.showEditText(_khhWidgetEntity),
              // Offstage(
              //   offstage: '是' == _zfbWidgetEntity.spinnerValue,
              //   child: WidgetUtils.showEditText(_khhWidgetEntity),
              // ),
              // WidgetUtils.showSpinner(
              //   _zfbWidgetEntity,
              //   stateCallback: (date) {
              //     setState(() {
              //       _zfbWidgetEntity.spinnerValue = date;
              //     });
              //   },
              // ),
              // WidgetUtils.showSpinner(
              //   _mrWidgetEntity,
              //   stateCallback: (date) {
              //     setState(() {
              //       _mrWidgetEntity.spinnerValue = date;
              //     });
              //   },
              // ),
              WidgetUtils.drawCodeDivider(height: 1),
              Container(
                alignment: Alignment.center,
                padding:
                    const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
                child: WidgetUtils.getTextColorView(
                  '请务必确认账号准确性，一经转账，无法退回',
                  Colours.red_606,
                  Dimens.FONT_SIZE_13,
                ),
              ),
              SizedBox(
                height: 32,
              ),
              _bomttonView(),
            ],
          ),
        ),
      ),
    );
  }

  // Widget _bankTypelistView() {
  //   return StreamBuilder(
  //     stream: _bloc.sendBankTypeData,
  //     initialData: _bankWidgetEntity.spinnerList,
  //     builder: (BuildContext contexts, AsyncSnapshot<Object> snapshot) {
  //       Object object = snapshot.data;
  //       if (object is List<IdNameEntity>) {
  //         _bankWidgetEntity.spinnerList?.clear();
  //         _bankWidgetEntity.spinnerList
  //           ..add(CommunitySeletionResultEntity()
  //             ..name = '请选择'
  //             ..id = '');
  //         object.forEach((element) {
  //           _bankWidgetEntity.spinnerList.add(CommunitySeletionResultEntity()
  //             ..id = element.id
  //             ..name = element.name);
  //         });
  //       }
  //       return WidgetUtils.showSpinner(
  //         _bankWidgetEntity,
  //         stateCallback: (date) {
  //           setState(() {
  //             _bankWidgetEntity.spinnerValue = date;
  //           });
  //         },
  //       );
  //     },
  //   );
  // }

  Widget _bomttonView() {
    return ButtonProgressView.crateCupertinoView('我已核对无误，确认提交',
        cupertinoCall: () {
      // String bankTypeName = _bankWidgetEntity.spinnerValue;
      // if (ObjectUtil.isEmptyString(bankTypeName)) {
      //   BotToast.showText(text: '账户类型不能未空');
      //   return;
      // }
      String zh = _zhWidgetEntity.controller.text;
      if (ObjectUtil.isEmptyString(zh)) {
        BotToast.showText(text: '账户不能未空');
        return;
      }

      String khh = _khhWidgetEntity.controller.text;
      if (ObjectUtil.isEmptyString(khh)) {
        BotToast.showText(text: '开户行不能未空');
        return;
      }

      Map<String, dynamic> data = {};

      data['id'] = '';
      data['userId'] = BaseConstant.userId;
      data['name'] = BaseConstant.name;
      data['idCard'] = BaseConstant.idCard;
      data['bankType'] = '';
      data['bankNo'] = zh;
      data['remark'] = ObjectUtil.isEmptyString(khh) ? '' : khh;
      data['ownerName'] = BaseConstant.name;
      data['ownerPhone'] = BaseConstant.phone;
      data['beDefault'] = 'Y';
      _bloc.addBankDate.add(data);
    });
  }

  @override
  void dispose() {
    _zhWidgetEntity.controller?.clear();
    _khhWidgetEntity.controller?.clear();
    _refresSubscription?.cancel();
    super.dispose();
  }
}
